//*********************************************************************
//		Copyright (c) 2003 - 2008 Microsoft Corporation.
//      This code is provided under Microsoft Public License, which can be found at
//      http://www.microsoft.com/resources/sharedsource/licensingbasics/publiclicense.mspx 
//
// @File: CfgForm.cs
//
// Purpose: Configuration_File/Edit Dialog
//
//
//
// Notes: 
//
//
//	
// History: 
//     @Version: V2.1
//
//     08/18/08 PGV 2.1.001.0 Starting V2.1 (based on 2.0.030.0 code)
//
// @EndHeader@
//*********************************************************************
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.IO;

namespace H2V2.ConfigUtility
{
	public class CfgForm : System.Windows.Forms.Form
	{
		private System.Windows.Forms.TextBox tbServer;
		private System.Windows.Forms.TextBox tbDb;
		private System.Windows.Forms.Button btCancel;
		private System.Windows.Forms.Button btSave;
		private System.Windows.Forms.TextBox tbFile;
		private System.Windows.Forms.Button btFileOpen;
		private System.Windows.Forms.ListBox lbTargets;
		private System.Windows.Forms.Button btAddTarget;
		private System.Windows.Forms.Button btRemoveTarget;
		private System.Windows.Forms.Button btAddFromFile;
		private System.Windows.Forms.TextBox tbNewTarget;
		private System.Windows.Forms.Label lServer;
		private System.Windows.Forms.Label lFile;
		private System.Windows.Forms.Label lDb;
		private System.Windows.Forms.Label lTargets;
		private System.Windows.Forms.Label lPerfProviders;
		private System.Windows.Forms.ListBox lbPerfProviders;
		private System.Windows.Forms.TextBox tbNewPerf;
		private System.Windows.Forms.Button btRemovePerf;
		private System.Windows.Forms.Button btAddPerf;
		private System.Windows.Forms.CheckBox cbExport;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public CfgForm()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			InitConfigurator (MainForm.curConfigFile);
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.tbServer = new System.Windows.Forms.TextBox();
			this.tbDb = new System.Windows.Forms.TextBox();
			this.lServer = new System.Windows.Forms.Label();
			this.btCancel = new System.Windows.Forms.Button();
			this.btSave = new System.Windows.Forms.Button();
			this.tbFile = new System.Windows.Forms.TextBox();
			this.lFile = new System.Windows.Forms.Label();
			this.btFileOpen = new System.Windows.Forms.Button();
			this.lbTargets = new System.Windows.Forms.ListBox();
			this.btAddTarget = new System.Windows.Forms.Button();
			this.btRemoveTarget = new System.Windows.Forms.Button();
			this.btAddFromFile = new System.Windows.Forms.Button();
			this.tbNewTarget = new System.Windows.Forms.TextBox();
			this.lDb = new System.Windows.Forms.Label();
			this.lTargets = new System.Windows.Forms.Label();
			this.lPerfProviders = new System.Windows.Forms.Label();
			this.lbPerfProviders = new System.Windows.Forms.ListBox();
			this.tbNewPerf = new System.Windows.Forms.TextBox();
			this.btRemovePerf = new System.Windows.Forms.Button();
			this.btAddPerf = new System.Windows.Forms.Button();
			this.cbExport = new System.Windows.Forms.CheckBox();
			this.SuspendLayout();
			// 
			// tbServer
			// 
			this.tbServer.Location = new System.Drawing.Point(120, 48);
			this.tbServer.Name = "tbServer";
			this.tbServer.Size = new System.Drawing.Size(144, 20);
			this.tbServer.TabIndex = 0;
			this.tbServer.Text = "";
			// 
			// tbDb
			// 
			this.tbDb.Location = new System.Drawing.Point(120, 80);
			this.tbDb.Name = "tbDb";
			this.tbDb.Size = new System.Drawing.Size(144, 20);
			this.tbDb.TabIndex = 1;
			this.tbDb.Text = "";
			// 
			// lServer
			// 
			this.lServer.Location = new System.Drawing.Point(24, 48);
			this.lServer.Name = "lServer";
			this.lServer.Size = new System.Drawing.Size(88, 23);
			this.lServer.TabIndex = 7;
			this.lServer.Text = "Server:";
			this.lServer.TextAlign = System.Drawing.ContentAlignment.TopRight;
			// 
			// btCancel
			// 
			this.btCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
			this.btCancel.Location = new System.Drawing.Point(296, 520);
			this.btCancel.Name = "btCancel";
			this.btCancel.Size = new System.Drawing.Size(104, 23);
			this.btCancel.TabIndex = 12;
			this.btCancel.Text = "Cancel";
			this.btCancel.Click += new System.EventHandler(this.btCancel_Click);
			// 
			// btSave
			// 
			this.btSave.Location = new System.Drawing.Point(136, 520);
			this.btSave.Name = "btSave";
			this.btSave.Size = new System.Drawing.Size(96, 23);
			this.btSave.TabIndex = 13;
			this.btSave.Text = "Save";
			this.btSave.Click += new System.EventHandler(this.btSave_Click);
			// 
			// tbFile
			// 
			this.tbFile.Location = new System.Drawing.Point(120, 16);
			this.tbFile.Name = "tbFile";
			this.tbFile.Size = new System.Drawing.Size(512, 20);
			this.tbFile.TabIndex = 14;
			this.tbFile.Text = "H2Config.xml";
			// 
			// lFile
			// 
			this.lFile.ImageAlign = System.Drawing.ContentAlignment.TopRight;
			this.lFile.Location = new System.Drawing.Point(8, 16);
			this.lFile.Name = "lFile";
			this.lFile.TabIndex = 15;
			this.lFile.Text = "File:";
			this.lFile.TextAlign = System.Drawing.ContentAlignment.TopRight;
			// 
			// btFileOpen
			// 
			this.btFileOpen.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(204)));
			this.btFileOpen.Location = new System.Drawing.Point(632, 16);
			this.btFileOpen.Name = "btFileOpen";
			this.btFileOpen.Size = new System.Drawing.Size(32, 23);
			this.btFileOpen.TabIndex = 16;
			this.btFileOpen.Text = "...";
			this.btFileOpen.Click += new System.EventHandler(this.btFileOpen_Click);
			// 
			// lbTargets
			// 
			this.lbTargets.Location = new System.Drawing.Point(120, 160);
			this.lbTargets.Name = "lbTargets";
			this.lbTargets.Size = new System.Drawing.Size(232, 251);
			this.lbTargets.TabIndex = 17;
			this.lbTargets.SelectedIndexChanged += new System.EventHandler(this.lbTargets_SelectedIndexChanged);
			// 
			// btAddTarget
			// 
			this.btAddTarget.Enabled = false;
			this.btAddTarget.Location = new System.Drawing.Point(376, 168);
			this.btAddTarget.Name = "btAddTarget";
			this.btAddTarget.Size = new System.Drawing.Size(88, 23);
			this.btAddTarget.TabIndex = 18;
			this.btAddTarget.Text = "Add";
			this.btAddTarget.Click += new System.EventHandler(this.btAddTarget_Click);
			// 
			// btRemoveTarget
			// 
			this.btRemoveTarget.Enabled = false;
			this.btRemoveTarget.Location = new System.Drawing.Point(376, 208);
			this.btRemoveTarget.Name = "btRemoveTarget";
			this.btRemoveTarget.Size = new System.Drawing.Size(88, 23);
			this.btRemoveTarget.TabIndex = 19;
			this.btRemoveTarget.Text = "Remove";
			this.btRemoveTarget.Click += new System.EventHandler(this.btRemoveTarget_Click);
			// 
			// btAddFromFile
			// 
			this.btAddFromFile.Location = new System.Drawing.Point(376, 248);
			this.btAddFromFile.Name = "btAddFromFile";
			this.btAddFromFile.Size = new System.Drawing.Size(88, 23);
			this.btAddFromFile.TabIndex = 20;
			this.btAddFromFile.Text = "Add From File";
			this.btAddFromFile.Click += new System.EventHandler(this.btAddFromFile_Click);
			// 
			// tbNewTarget
			// 
			this.tbNewTarget.Location = new System.Drawing.Point(488, 168);
			this.tbNewTarget.Name = "tbNewTarget";
			this.tbNewTarget.Size = new System.Drawing.Size(176, 20);
			this.tbNewTarget.TabIndex = 21;
			this.tbNewTarget.Text = "";
			this.tbNewTarget.TextChanged += new System.EventHandler(this.tbNewTarget_TextChanged);
			// 
			// lDb
			// 
			this.lDb.Location = new System.Drawing.Point(8, 80);
			this.lDb.Name = "lDb";
			this.lDb.TabIndex = 8;
			this.lDb.Text = "Database:";
			this.lDb.TextAlign = System.Drawing.ContentAlignment.TopRight;
			// 
			// lTargets
			// 
			this.lTargets.Location = new System.Drawing.Point(8, 160);
			this.lTargets.Name = "lTargets";
			this.lTargets.TabIndex = 22;
			this.lTargets.Text = "Targets:";
			this.lTargets.TextAlign = System.Drawing.ContentAlignment.TopRight;
			// 
			// lPerfProviders
			// 
			this.lPerfProviders.Location = new System.Drawing.Point(8, 424);
			this.lPerfProviders.Name = "lPerfProviders";
			this.lPerfProviders.TabIndex = 24;
			this.lPerfProviders.Text = "PerfProviders:";
			this.lPerfProviders.TextAlign = System.Drawing.ContentAlignment.TopRight;
			// 
			// lbPerfProviders
			// 
			this.lbPerfProviders.Location = new System.Drawing.Point(120, 424);
			this.lbPerfProviders.Name = "lbPerfProviders";
			this.lbPerfProviders.Size = new System.Drawing.Size(232, 69);
			this.lbPerfProviders.TabIndex = 23;
			this.lbPerfProviders.SelectedIndexChanged += new System.EventHandler(this.lbPerfProviders_SelectedIndexChanged);
			// 
			// tbNewPerf
			// 
			this.tbNewPerf.Location = new System.Drawing.Point(488, 424);
			this.tbNewPerf.Name = "tbNewPerf";
			this.tbNewPerf.Size = new System.Drawing.Size(176, 20);
			this.tbNewPerf.TabIndex = 27;
			this.tbNewPerf.Text = "";
			this.tbNewPerf.TextChanged += new System.EventHandler(this.tbNewPerf_TextChanged);
			// 
			// btRemovePerf
			// 
			this.btRemovePerf.Enabled = false;
			this.btRemovePerf.Location = new System.Drawing.Point(376, 464);
			this.btRemovePerf.Name = "btRemovePerf";
			this.btRemovePerf.Size = new System.Drawing.Size(88, 23);
			this.btRemovePerf.TabIndex = 26;
			this.btRemovePerf.Text = "Remove";
			this.btRemovePerf.Click += new System.EventHandler(this.btRemovePerf_Click);
			// 
			// btAddPerf
			// 
			this.btAddPerf.Enabled = false;
			this.btAddPerf.Location = new System.Drawing.Point(376, 424);
			this.btAddPerf.Name = "btAddPerf";
			this.btAddPerf.Size = new System.Drawing.Size(88, 23);
			this.btAddPerf.TabIndex = 25;
			this.btAddPerf.Text = "Add";
			this.btAddPerf.Click += new System.EventHandler(this.btAddPerf_Click);
			// 
			// cbExport
			// 
			this.cbExport.Location = new System.Drawing.Point(120, 120);
			this.cbExport.Name = "cbExport";
			this.cbExport.TabIndex = 29;
			this.cbExport.Text = "Export Data";
			this.cbExport.CheckedChanged += new System.EventHandler(this.cbExport_CheckedChanged);
			// 
			// CfgForm
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(712, 558);
			this.Controls.Add(this.cbExport);
			this.Controls.Add(this.tbNewPerf);
			this.Controls.Add(this.tbNewTarget);
			this.Controls.Add(this.tbFile);
			this.Controls.Add(this.tbDb);
			this.Controls.Add(this.tbServer);
			this.Controls.Add(this.btRemovePerf);
			this.Controls.Add(this.btAddPerf);
			this.Controls.Add(this.lPerfProviders);
			this.Controls.Add(this.lbPerfProviders);
			this.Controls.Add(this.lTargets);
			this.Controls.Add(this.btAddFromFile);
			this.Controls.Add(this.btRemoveTarget);
			this.Controls.Add(this.btAddTarget);
			this.Controls.Add(this.lbTargets);
			this.Controls.Add(this.btFileOpen);
			this.Controls.Add(this.lFile);
			this.Controls.Add(this.btSave);
			this.Controls.Add(this.btCancel);
			this.Controls.Add(this.lDb);
			this.Controls.Add(this.lServer);
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
			this.Name = "CfgForm";
			this.Text = "Edit Configuration File";
			this.ResumeLayout(false);

		}
		#endregion

		private void btSave_Click(object sender, System.EventArgs e)
		{
			try
			{
				MainForm.Cfg.SetRepository (tbServer.Text, tbDb.Text);
				MainForm.Cfg.SetTargets ();
				MainForm.Cfg.SetPerfProviders ();
				MainForm.Cfg.SetSettings ();

				MainForm.Cfg.Save ();

				this.Close ();
			}
			catch (Exception ex)
			{
				MessageBox.Show (ex.Message, "Saving XML", MessageBoxButtons.OK, MessageBoxIcon.Error);
			}

		}

		private void btFileOpen_Click(object sender, System.EventArgs e)
		{
			OpenFileDialog ofd = new OpenFileDialog ();

			ofd.InitialDirectory = ".";
			ofd.FileName = tbFile.Text;

			if (ofd.ShowDialog () == DialogResult.OK)
			{
				if (InitConfigurator (ofd.FileName))
				{
					tbFile.Text = ofd.FileName;
				}
			}
		}

		bool InitConfigurator (string a_filename)
		{
			try 
			{
				MainForm.Cfg.Init (a_filename);
			
				tbServer.Text = MainForm.Cfg.Server;
				tbDb.Text = MainForm.Cfg.Database;

				cbExport.Checked = MainForm.Cfg.bExportData;
				

				if (lbTargets.Items.Count > 0)
				{
					lbTargets.Items.Clear ();
				}
				foreach (string cmp in MainForm.Cfg.Computers_array)
				{
					lbTargets.Items.Add (cmp);
				}

				if (lbPerfProviders.Items.Count > 0)
				{
					lbPerfProviders.Items.Clear ();
				}
				foreach (string cmp in MainForm.Cfg.Perf_array)
				{
					lbPerfProviders.Items.Add (cmp);
				}

				MainForm.curConfigFile = a_filename;
				return true;
			}
			catch (Exception ex)
			{
				MessageBox.Show (ex.Message, "Opening XML", MessageBoxButtons.OK, MessageBoxIcon.Error);
				return false;
			}
		}

		private void btAddTarget_Click(object sender, System.EventArgs e)
		{
			if (MainForm.ValidateName (tbNewTarget.Text))
			{
				if (MainForm.Cfg.AddTarget (tbNewTarget.Text))
				{
					lbTargets.Items.Add (tbNewTarget.Text.ToUpper ());
					tbNewTarget.Text = "";
				}
				else
				{
					MessageBox.Show ("Target is already on the list", "Adding Target", MessageBoxButtons.OK, MessageBoxIcon.Error);
				}
			}
			else
			{
				MessageBox.Show ("Invalid computer name \""+tbNewTarget.Text+"\"", "Adding Target", MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
		}

		private void btRemoveTarget_Click(object sender, System.EventArgs e)
		{
			string name = (string)lbTargets.SelectedItem;
			lbTargets.Items.RemoveAt (lbTargets.SelectedIndex);
			MainForm.Cfg.RemoveTarget (name);
		}

		private void btAddFromFile_Click(object sender, System.EventArgs e)
		{
			OpenFileDialog ofd = new OpenFileDialog ();

			ofd.InitialDirectory = ".";
			ofd.Filter = "txt files (*.txt)|*.txt";

			if (ofd.ShowDialog () == DialogResult.OK)
			{
				LoadFromFile (ofd.FileName);
			}
		}

		void LoadFromFile (string a_filename)
		{
			string s = "";
			int len = 0;

			ArrayList buff_array = new ArrayList (16);

			StreamReader sr = new StreamReader (a_filename);

			s = sr.ReadLine ();
			while (s != null)
			{
				s = s.TrimEnd ().TrimStart (); 
				if (s != "")
				{
					if (!MainForm.ValidateName (s))
					{
						len = (s.Length > 128) ? 128 : s.Length;
						DialogResult dr = MessageBox.Show ("Invalid computer name \""+s.Substring (0,len)+"\" \r\nThe name will be ignored.\r\n"
							+"Do you want to continue to process the file?", "Adding Targets from File", MessageBoxButtons.YesNo, MessageBoxIcon.Error);
						if (dr == DialogResult.No)
						{
							return;
						}
					}
					else 
					{
						buff_array.Add (s.ToUpper ());
					}
				}

				s = sr.ReadLine ();
			}

			foreach (string t in buff_array)
			{
				if (MainForm.Cfg.AddTarget (t))
				{
					lbTargets.Items.Add (t);
				}
			}
		}

		private void tbNewTarget_TextChanged(object sender, System.EventArgs e)
		{
			btAddTarget.Enabled = (tbNewTarget.Text.Length > 0);
		}

		private void lbTargets_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			btRemoveTarget.Enabled = (lbTargets.SelectedIndex >= 0);
		}

		private void btCancel_Click(object sender, System.EventArgs e)
		{
			Close ();
		}

		private void btAddPerf_Click(object sender, System.EventArgs e)
		{
			if (tbNewPerf.Text != "")
			{
				if (MainForm.Cfg.AddPerfProvider (tbNewPerf.Text))
				{
					lbPerfProviders.Items.Add (tbNewPerf.Text.ToUpper ());
					tbNewPerf.Text = "";
				}
				else
				{
					MessageBox.Show ("PerfProvider is already on the list", "Adding PerfProider", MessageBoxButtons.OK, MessageBoxIcon.Error);
				}
			}
		}

		private void btRemovePerf_Click(object sender, System.EventArgs e)
		{
			string name = (string)lbPerfProviders.SelectedItem;
			lbPerfProviders.Items.RemoveAt (lbPerfProviders.SelectedIndex);
			MainForm.Cfg.RemovePerfProvider (name);
		}

		private void tbNewPerf_TextChanged(object sender, System.EventArgs e)
		{
			btAddPerf.Enabled = (tbNewPerf.Text.Length > 0);
		}

		private void lbPerfProviders_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			btRemovePerf.Enabled = (lbPerfProviders.SelectedIndex >= 0);
		}

		private void cbExport_CheckedChanged(object sender, System.EventArgs e)
		{
			MainForm.Cfg.bExportData = cbExport.Checked;
		}
	}
}
